package com.pt25.service.sys.application.impl;

import com.pt25.base.exception.BizException;
import com.pt25.base.model.common.constants.SecurityConstant;
import com.pt25.base.model.security.UserSession;
import com.pt25.base.util.PageUtil;
import com.pt25.base.util.TreeUtil;
import com.pt25.service.sys.application.AdminApp;
import com.pt25.service.sys.domain.admin.ddo.AdminPermission;
import com.pt25.service.sys.domain.admin.ddo.AdminRole;
import com.pt25.service.sys.domain.admin.ddo.AdminUser;
import com.pt25.service.sys.domain.admin.factory.AdminFactory;
import com.pt25.service.sys.domain.admin.service.AdminPermissionService;
import com.pt25.service.sys.domain.admin.service.AdminRoleService;
import com.pt25.service.sys.domain.admin.service.AdminUserService;
import com.pt25.service.sys.interfaces.vo.admin.permission.AdminPermissionGetVo;
import com.pt25.service.sys.interfaces.vo.admin.permission.AdminPermissionTreeVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class AdminAppImpl implements AdminApp {

    @Resource
    private AdminFactory adminFactory;

    @Resource
    private AdminUserService adminUserService;

    @Resource
    private AdminRoleService adminRoleService;

    @Resource
    private AdminPermissionService adminPermissionService;

    @Override
    public UserSession<?> createSessionData(AdminUser adminUser) {
        AdminUser loginAdminUser = adminUserService.getLoginAdminUser(adminUser);
        if (null != loginAdminUser) {
            List<AdminRole> adminRoles = adminRoleService.getAdminRolesByUserId(loginAdminUser.getId());
            Set<String> permissions = adminPermissionService.getPermissionsByRoleIds(adminRoles.stream().map(AdminRole::getId).collect(Collectors.toSet()));
            return UserSession.builder()
                    .uid(loginAdminUser.getId() + "")
                    .userType(SecurityConstant.ADMIN)
                    .roleList(adminRoles.stream().map(AdminRole::getRole).collect(Collectors.toSet()))
                    .permissionList(permissions)
                    .build();
        }
        throw new BizException("用户名或密码错误！");
    }

    @Override
    public void addAdminUser(AdminUser adminUser) {
        adminUserService.addAdminUser(adminUser);
    }

    @Override
    public void delAdminUser(Long id) {
        adminUserService.delAdminUser(id);
    }

    @Override
    public void updateAdminUser(AdminUser adminUser) {
        adminUserService.updateAdminUser(adminUser);
    }

    @Override
    public AdminUser getAdminUser(Long id) {
        return adminUserService.getAdminUser(id);
    }

    @Override
    public PageUtil.PageVo pageAdminUser(AdminUser adminUser) {
        return adminUserService.pageAdminUser(adminUser);
    }

    @Override
    public void addAdminRole(AdminRole adminRole) {
        adminRoleService.addAdminRole(adminRole);
    }

    @Override
    public void delAdminRole(Long id) {
        adminRoleService.delAdminRole(id);
    }

    @Override
    public void updateAdminRole(AdminRole adminRole) {
        adminRoleService.updateAdminRole(adminRole);
    }

    @Override
    public AdminRole getAdminRole(Long id) {
        return adminRoleService.getAdminRole(id);
    }

    @Override
    public PageUtil.PageVo pageAdminRole(AdminRole adminRole) {
        return adminRoleService.pageAdminRole(adminRole);
    }

    @Override
    public void addAdminPermission(AdminPermission adminPermission) {
        adminPermissionService.addAdminPermission(adminPermission);
    }

    @Override
    public void delAdminPermission(Long id) {
        adminPermissionService.delAdminPermission(id);
    }

    @Override
    public void updateAdminPermission(AdminPermission adminPermission) {
        adminPermissionService.updateAdminPermission(adminPermission);
    }

    @Override
    public AdminPermissionGetVo getAdminPermission(Long id) {
        return adminFactory.adminPermission2AdminPermissionVO(adminPermissionService.getAdminPermission(id), AdminPermissionGetVo.class);
    }

    @Override
    public List<AdminPermissionTreeVo> treeAdminPermission() {
        List<AdminPermissionTreeVo> adminPermissionTreeVos = new ArrayList<>();
        List<AdminPermission> adminPermissions = adminPermissionService.listAdminPermission();
        adminPermissions.forEach(a -> adminPermissionTreeVos.add(adminFactory.adminPermission2AdminPermissionVO(a, AdminPermissionTreeVo.class)));
        return TreeUtil.toTree(adminPermissionTreeVos, 0L);
    }

}
